<template>
  <div class="list-page">
    <Row>
      <div class="toolbar">
        <div>
          <Button
            @click="association(bloc.form.masterId)"
            type="primary"
            icon="md-add"
            >关联经销商
          </Button>
        </div>
        <Input
          placeholder="经销商"
          v-model="bloc.form.agentName"
          style="width: 180px; margin-right: 10px"
        />
        <DatePicker
          type="daterange"
          placeholder="添加时间"
          format="yyyy/MM/dd"
          v-model="bloc.form.created"
          :editable="false"
          style="width: 200px; margin-right: 10px"
        ></DatePicker>
        <Button
          type="primary"
          icon="ios-search"
          style="margin-right: 10px"
          size="default"
          @click="bloc.load(1, bloc.paged.pageSize,bloc.form.masterId,bloc.form.agentName)"
        >查询
        </Button>
        <Button
          icon="md-refresh"
          style="margin-right: 10px"
          size="default"
          @click="bloc.reset()"
        >重置</Button
        >
      </div>
    </Row>
    <Row>
      <Table
        border
        :loading="bloc.loading"
        :columns="columns"
        highlight-row
        :data="bloc.paged.items"

      >
        <template slot-scope="{ row }" slot="action">
          <div style="white-space: nowrap">
           <!-- <Button
             size="small"
             @click="modifyItem(row)"
             type="info"
             v-if="Validation.check(Resources.MEMBER_UPDATE)"
           >
             编辑
           </Button> -->
           <Button
              size="small"
              @click="removeItem(row)"
              type="warning"
              v-if="Validation.check(Resources.ACCOUNT_DELETE)"
            >
              删除
            </Button>
          </div>
        </template>
        <slot name="footer" slot="footer">
          <Page
            show-elevator
            show-total
            show-sizer
            :total="bloc.paged.itemCount"
            :current="bloc.paged.pageIndex"
            :page-size="bloc.paged.pageSize"
            :page-size-opts="[15, 30, 50, 100]"
            @on-change="(val) => bloc.load(val, bloc.paged.pageSize,bloc.form.masterId,bloc.form.agentName)"
            @on-page-size-change="(val) => bloc.load(bloc.paged.pageIndex, val,bloc.form.masterId,bloc.form.agentName)"
          />
        </slot>
      </Table>
    </Row>
  </div>
</template>

<script lang="ts">
    import { Component, Vue } from 'vue-property-decorator';
    import { Route } from 'vue-router';
    import { ListBloc } from '@/view/agents/agent/ListBloc';
    import { Family } from '@/base/model/Family';
    import { RoleRelationship } from '@/base/model/RoleRelationship';
    import { formatDate } from '@/base/util/Dates';
    import { Validation } from '@/base/Validation';
    import { Resources } from '@/base/Resources';
    import { isEmpty } from '@/base/util/Strings';
    import { store } from '@/store/Store';

    const columns = (bloc: ListBloc) => [
        {
            title: 'id',
            key: 'id',
            align: 'left',
            minWidth: 120,
            tooltip: true
        },
        {
            title: '管理员',
            key: 'userName',
            align: 'left',
            minWidth: 120,
            tooltip: true
        },
        {
            title: '经销商',
            key: 'agentName',
            align: 'left',
            minWidth: 120,
            tooltip: true
        },
        {
            title: '创建时间',
            render: (h: any, params: any) => {
                return h('span', {
                    domProps: {
                        innerText: formatDate(
                            new Date(params.row.created),
                            'yyyy-MM-dd hh:mm:ss'
                        )
                    }
                });
            },
            width: 160,
            align: 'left',
            tooltip: true
        },
        {
            title: '更新时间',
            render: (h: any, params: any) => {
                return h('span', {
                    domProps: {
                        innerText: formatDate(
                            new Date(params.row.updated),
                            'yyyy-MM-dd hh:mm:ss'
                        )
                    }
                });
            },
            width: 160,
            align: 'left',
            tooltip: true
        },
        {
            title: '操作',
            slot: 'action',
            width: 47 * 2 + 75 + 20,
            align: 'left',
            fixed: 'right'
        }
    ];
   
    @Component({
        components: {}
    })
    export default class extends Vue {
        beforeRouteEnter(to: Route, from: Route, next: (vm: any) => void) {
            next((vm) => {
                store.state.prePath.url = "/agents"
                vm.bloc.form.masterId = vm.idx;
                vm.bloc
                    .load(vm.bloc.paged.pageIndex, vm.bloc.paged.pageSize,vm.bloc.form.masterId,vm.bloc.form.agentName)
                    .catch((e) => {
                        vm.$Message.error('操作错误');
                    });
            });
        }

      get idx() {
        return Number(this.$route.params.id);
      }

        bloc: ListBloc = new ListBloc();
        columns = columns(this.bloc);
        Validation = Validation;
        Resources = Resources;

        removeItem(dataItem: RoleRelationship) {
            this.$Modal.confirm({
                title: "提示",
                content: "确认删除后将无法恢复！还要继续吗？",
                okText: "删除",
                cancelText: "取消",
                onOk: () => {
                    this.bloc
                        .removeItem(dataItem)
                        .then((value) => {
                            this.bloc.load(this.bloc.paged.pageIndex, this.bloc.paged.pageSize,this.bloc.form.masterId,this.bloc.form.agentName);
                        })
                        .then(() => {
                            this.$Message.success("删除成功");
                        })
                        .catch((e) => {
                            this.$Message.error("删除失败");
                        });
                },
            });
        }

        association(id) {
        this.$router.replace(`/agentss/add/${id}`);
      }
    }
</script>

<style lang="less">
</style>
